//
//  GNStatusRetweetView.swift
//  SinaWeChat
//
//  Created by mac on 15/12/10.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SnapKit
class GNStatusRetweetView: UIView {
    
    //当前视图的底部约束
    var bottomConstraint : Constraint?

    var statusViewModel:GNStatusViewModel? {
        
        didSet{
            
            contentLabel.text = statusViewModel?.retweetText
            
            //判断是否有图,如果有图
            // --> 1.让其显示出来; 2.更新约束 3.设置数据
            self.bottomConstraint?.uninstall()
            
            if let pic_urls = statusViewModel?.status?.pic_urls where pic_urls.count > 0{
                
                //设置数据
                pictureView.pic_urls = pic_urls
                
                //显示
                pictureView.hidden = false
                
                //更新约束
                snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraint = make.bottom.equalTo(pictureView).offset(GNStatusCellMargin).constraint
                })
                
            }else{
                
                // 1. 让其隐藏 ,2更新约束
                pictureView.hidden = true
                
                //更新约束
                
                snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraint = make.bottom.equalTo(contentLabel).offset(GNStatusCellMargin).constraint
                })
            }
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //设置视图内容
    
    private func setupUI()
    {
        //添加控件
        addSubview(contentLabel)
        //添加控件
        addSubview(pictureView)
        
        //添加约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(self).offset(GNStatusCellMargin)
            make.top.equalTo(self).offset(GNStatusCellMargin)
        }
        
        //配图的约束
<<<<<<< HEAD
        pictureView.snp_makeConstraints { (make) -> Void in
=======
        contentLabel.snp_makeConstraints { (make) -> Void in
>>>>>>> 009be840054e3af11221fb27da600fe2ee052d08
            
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(GNStatusCellMargin)
            
        }
        
        //当前控件的高与内容的高度是一样的
        
        self.snp_makeConstraints { (make) -> Void in
            
             self.bottomConstraint =  make.bottom.equalTo(pictureView).offset(GNStatusCellMargin).constraint
        }
        
    
        
    }
    
    
    //MARK : - 懒加载
    private lazy var contentLabel :UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15, maxWidth: SCREENW - 2 * GNStatusCellMargin)
    
    //配图 View
    private lazy var pictureView: GNStatusPictureView = {
        
        let view = GNStatusPictureView()
        
        view.backgroundColor = self.backgroundColor
        
        return view
        
    }()
    
}
